package com.lethelh.jdbc.app.command;

import com.lethelh.jdbc.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 查询商品
 * @author jasonper
 */
public class QueryCommand implements Command {

    @Override
    public void execute() {
        String sql = "select * from goods";

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtils.getConnection();
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                double price = rs.getFloat(3);
                String desc = rs.getString("desp");
                System.out.println("商品编号:"+id+" 商品名称:"+name+" 商品价格:"+price+" 商品描述:"+desc);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                DBUtils.closeConnection(rs, stmt, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
